package operator.condition;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.BiPredicate;
import util.RxJavaHelper;
import util.ThreadHelper;

class SequenceEqualOperator {

    public static void main(String[] args) {
//        sequenceEqual1();
        sequenceEqual2();
    }

    /**
     * 传递两个Observable给SequenceEqual操作符，它会比较两个Observable的发射物，
     * 如果两个序列是相同的（相同的数据，相同的顺序，相同的终止状态），它就发射true，否则发射false
     */
    static void sequenceEqual1() {
        Observable.sequenceEqual(
                Observable.just(0L, 1L, 2L),
                Observable.intervalRange(0, 3, 0, 1, TimeUnit.SECONDS))
                .subscribe(RxJavaHelper.getSingleConsumer());
        ThreadHelper.sleep();
    }

    /**
     * 自定义比对规则，前提是元素数得一样
     */
    static void sequenceEqual2() {
        Observable.sequenceEqual(
                Observable.just(0l, 1l, 2l, 3l),
                Observable.intervalRange(4, 4, 0, 1, TimeUnit.SECONDS), new BiPredicate<Long, Long>() {
                    @Override
                    public boolean test(Long aLong, Long aLong2) throws Exception {
                        return true;
                    }
                })
                .subscribe(RxJavaHelper.getSingleConsumer());
        ThreadHelper.sleep();
    }



}
